﻿using System;
using System.Reflection;
using System.Threading.Tasks;

namespace Wireframe
{
    public static class Wire
    {
        private static WireHub _wireHub;

        public static void Initialize(params Assembly[] assemblies)
        {
            _wireHub = new WireHub(new EventHandlers(assemblies ?? AppDomain.CurrentDomain.GetAssemblies()));
        }

        public static T Register<T>(this T instance)
        {
            if (_wireHub == null)
            {
                throw new ApplicationException("Initialize Wireframe before use!");
            }

            return _wireHub.Register(instance);
        }

        public static T Unregister<T>(this T instance)
        {
            if (_wireHub == null)
            {
                throw new ApplicationException("Initialize Wireframe before use!");
            }

            return _wireHub.Unregister(instance);
        }

        public static Task Send<T>(this object instance, T message)
        {
            if (_wireHub == null)
            {
                throw new ApplicationException("Initialize Wireframe before use!");
            }

            return _wireHub.Send(message);
        }

        public static Task Send<T>(T message)
        {
            if (_wireHub == null)
            {
                throw new ApplicationException("Initialize Wireframe before use!");
            }

            return Send(null, message);
        }
    }
}